﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Collections;
using System.Web.Security;

public partial class za_zeni : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ArrayList lista;
        if (Session["kosnicka"] != null)
        {
            int vkupno = 0;
            lista = (ArrayList)Session["kosnicka"];
            lstKupi.DataSource = lista;
            lstKupi.DataBind();
            foreach (ListItem l1 in lista)
                vkupno += Convert.ToInt32(l1.Value);
            lblVkupno.Text = "Вкупна цена: " + vkupno.ToString();
        }
        if (isUserLoggedIn())
        {
            btnOdlogiranje.Visible = true;
            lblKorisnik.Text ="Најавен корисник: "+ (string)Session["korisnik"];
            ispolniGrid();
            SqlConnection konekcija = new SqlConnection();
            konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
            string sqlString = "SELECT ime FROM Info";
            SqlCommand komanda = new SqlCommand(sqlString, konekcija);
            try
            {
                konekcija.Open();
                SqlDataReader citac = komanda.ExecuteReader();
                while (citac.Read())
                {
                    lblNaslov.Text = citac["ime"].ToString();
                }
                citac.Close();
            }
            catch (Exception err)
            {
                lblNaslov.Text = err.Message;
            }
            finally
            {
                konekcija.Close();
            }
            return;

        }
        else
        {
            btnLogiranje.Visible = true;
        }
        if (!IsPostBack)
        {
            SqlConnection konekcija = new SqlConnection();
            konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
            string sqlString = "SELECT ime FROM Info";
            SqlCommand komanda = new SqlCommand(sqlString, konekcija);
            try
            {
                konekcija.Open();
                SqlDataReader citac = komanda.ExecuteReader();
                while (citac.Read())
                {
                    lblNaslov.Text = citac["ime"].ToString();
                }
                citac.Close();
            }
            catch (Exception err)
            {
                lblNaslov.Text = err.Message;
            }
            finally
            {
                konekcija.Close();
            }
        }
        ispolniGrid();
    }
    private bool isUserLoggedIn()
    {
        if (HttpContext.Current.User != null)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                return true;
            }
        }
        return false;
    }
    protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
    {
        if (Menu1.SelectedValue == "Маици")
            Response.Redirect("za_mazi.aspx?tip=maici");
        if (Menu1.SelectedValue == "Патики")
            Response.Redirect("za_mazi.aspx?tip=patiki");
        if (Menu1.SelectedValue == "Панталони")
            Response.Redirect("za_mazi.aspx?tip=pantaloni");
        if (Menu1.SelectedValue == "Фустани")
            Response.Redirect("za_zeni.aspx?tip=fustani");
        if (Menu1.SelectedValue == "Штикли")
            Response.Redirect("za_zeni.aspx?tip=stikli");
        if (Menu1.SelectedValue == "Капи")
            Response.Redirect("za_zeni.aspx?tip=kapi");
        if (Menu1.SelectedValue == "Облека за мажи")
            Response.Redirect("za_mazi.aspx?tip=mazi");
        if (Menu1.SelectedValue == "Облека за жени")
            Response.Redirect("za_zeni.aspx?tip=zeni");
    }
    protected void ispolniGrid()
    {
        string tip = Convert.ToString(Request.QueryString["tip"]);
        if (tip == "fustani")
        {
            SqlConnection konekcija = new SqlConnection();
            konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
            string sqlString = "SELECT * FROM Za_zenski";
            SqlCommand komanda = new SqlCommand(sqlString, konekcija);
            SqlDataAdapter adapter = new SqlDataAdapter(komanda);
            DataSet ds = new DataSet();
            try
            {
                konekcija.Open();
                adapter.Fill(ds, "fustani");
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
            catch (Exception err)
            {
            }
            finally
            {
                konekcija.Close();
            }
        }
        if (tip == "stikli")
        {
            SqlConnection konekcija = new SqlConnection();
            konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
            string sqlString = "SELECT * FROM Stikli";
            SqlCommand komanda = new SqlCommand(sqlString, konekcija);
            SqlDataAdapter adapter = new SqlDataAdapter(komanda);
            DataSet ds = new DataSet();
            try
            {
                konekcija.Open();
                adapter.Fill(ds, "stikli");
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
            catch (Exception err)
            {
            }
            finally
            {
                konekcija.Close();
            }
        }
        if (tip == "kapi")
        {
            SqlConnection konekcija = new SqlConnection();
            konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
            string sqlString = "SELECT * FROM Pantaloni";
            SqlCommand komanda = new SqlCommand(sqlString, konekcija);
            SqlDataAdapter adapter = new SqlDataAdapter(komanda);
            DataSet ds = new DataSet();
            try
            {
                konekcija.Open();
                adapter.Fill(ds, "pantaloni");
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
            catch (Exception err)
            {
            }
            finally
            {
                konekcija.Close();
            }
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string tip = Convert.ToString(Request.QueryString["tip"]);
        if (tip == "fustani")
        {
            Fustan.ImageUrl = GridView1.SelectedRow.Cells[4].Text;
            Fustan.Visible = true;
        }
        if (tip == "stikli")
        {
            Stikla.ImageUrl = GridView1.SelectedRow.Cells[4].Text;
            Stikla.Visible = true;
        }
        if (tip == "kapi")
        {
            Glava.ImageUrl = GridView1.SelectedRow.Cells[4].Text;
            Glava.Visible = true;
        }
    }
    protected void btnLogiranje_Click(object sender, EventArgs e)
    {
        Response.Redirect("Login.aspx");
    }
    protected void btnOdlogiranje_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();

        // kolaceto istekuva vcera, t.e. ne e validno
        if (FormsAuthentication.FormsCookieName != null)
        {
            Context.Response.Cookies[FormsAuthentication.FormsCookieName].Expires = DateTime.Now.AddDays(-1);
        }

        // prefrli na default stranata
        Response.Redirect("Doma.aspx");
    }
    protected void btnDodadi_Click(object sender, EventArgs e)
    {
        ArrayList lista;
        if (Session["kosnicka"] == null)
        {
            lista = new ArrayList();
        }
        else
        {
            lista = (ArrayList)Session["kosnicka"];
        }
        lista.Add(new ListItem(GridView1.SelectedRow.Cells[2].Text, GridView1.SelectedRow.Cells[3].Text));
        lstKupi.DataSource = lista;
        lstKupi.DataBind();
        Session["kosnicka"] = lista;
        lblVkupno.Text ="Вкупна цена: "+ GridView1.SelectedRow.Cells[3].Text;
        if (Session["kosnicka"] != null)
        {
            int cena = 0;
            foreach (ListItem l1 in lista)
                cena += Convert.ToInt32(l1.Value);
            lblVkupno.Text ="Вкупна цена: "+ cena.ToString();
        }
        dodadi_pom();
    }
    protected void dodadi_pom()
    {
        string ime;
        int cena, broj;
        SqlConnection konekcija = new SqlConnection();
        konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
        ime = GridView1.SelectedRow.Cells[2].Text;
        broj = Convert.ToInt32(GridView1.SelectedRow.Cells[1].Text);
        cena = Convert.ToInt32(GridView1.SelectedRow.Cells[3].Text);
        string sqlString = "INSERT INTO pom values('" + broj + "','" + ime + "','" + cena + "')";
        SqlCommand komanda = new SqlCommand(sqlString, konekcija);
        try
        {
            konekcija.Open();
            komanda.ExecuteNonQuery();
        }
        catch (Exception err)
        {
        }
        finally
        {
            konekcija.Close();
        }
    }
    protected void izbrisi_pom()
    {
        SqlConnection konekcija = new SqlConnection();
        konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
        string sqlString = "DELETE FROM pom";
        SqlCommand komanda = new SqlCommand(sqlString, konekcija);
        try
        {
            konekcija.Open();
            komanda.ExecuteNonQuery();
        }
        catch (Exception err)
        {
        }
        finally
        {
            konekcija.Close();
        }
    }
    protected void btnIsprazni_Click(object sender, EventArgs e)
    {
        izbrisi_pom();
        lstKupi.Items.Clear();
        lblVkupno.Text = "";
        
        Session["kosnicka"] = null;
        
    }
    protected void btnKupi_Click(object sender, EventArgs e)
    {
        if (isUserLoggedIn())
        {
            Response.Redirect("Kupi.aspx");
            return;
        }
        else
        {
            Response.Redirect("Login.aspx");
        }
    }
}